<template>
  <div class="app-container">
    <ContentWrap>
      <CustomBox>
        <el-row :gutter="20">
          <el-col :span="4">
            <RegionTree node-type="1" :node-click="handleDfqNodeClick"/>
          </el-col>

          <el-col :span="20" class="content-container">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                     label-width="68px">
              <el-form-item label="报表类型" prop="frozenType">
                <el-radio-group v-model="queryParams.frozenType" @change="handleFrozenTypeChange">
                  <el-radio label="day">日</el-radio>
                  <el-radio label="month">月</el-radio>
                  <!--              <el-radio label="year">年</el-radio>-->
                </el-radio-group>
              </el-form-item>
              <el-form-item label="开始日期" prop="startDate">
                <el-date-picker v-model="queryParams.startDate" :type="dateType" :value-format="valueType"
                                placeholder="选择日期"></el-date-picker>
              </el-form-item>
              <el-form-item label="结束日期" prop="endDate">
                <el-date-picker v-model="queryParams.endDate" :type="dateType" :value-format="valueType"
                                placeholder="选择日期"></el-date-picker>
              </el-form-item>
              <el-form-item>
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出
                </el-button>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>

            <el-table v-loading="loading" :data="freezeDfqList" border :height="getAvailableHeight">
              <!--      <el-table-column type="selection" width="55" align="center"/>-->
              <!--      <el-table-column label="设备归属租户" align="center" prop="tenantId"/>-->
              <!--      <el-table-column label="层级id" align="center" prop="areaid"/>-->
              <el-table-column type="index" width="40" align="center" label="#" :fixed="true"/>
              <el-table-column label="名称" align="left" prop="zone.areaName"/>
              <el-table-column label="日期" align="center" prop="freezetime" width="100"/>
              <!--      <el-table-column label="冻结类型，日：day，月：month" align="center" prop="frozenType"/>-->
              <el-table-column label="供水量(m³)" align="center" prop="inputFlow" width="120" sortable/>
              <el-table-column label="环比(%)" align="center" prop="hb" width="100" sortable>
                <template slot-scope="scope">
                  <span :style="getHbStyle(scope.row.hb)">{{ scope.row.hb }}</span>
                </template>
              </el-table-column>
              <el-table-column label="售水量(m³)" align="center" prop="saleWater" width="120" sortable/>
              <el-table-column label="无收益水量(m³)" align="center" prop="freeWater" width="140" sortable/>
              <el-table-column label="产销差率(%)" align="center" prop="cxcl" width="130" sortable>
                <template slot-scope="scope">
                  <span :style="getCxclStyle(scope.row.cxcl)">{{ scope.row.cxcl }}</span>
                </template>
              </el-table-column>
              <el-table-column v-if="queryParams.frozenType==='day'" label="背景漏失(m³/KM/天)" align="center"
                               prop="uarl"
                               width="170" sortable/>
              <el-table-column v-if="queryParams.frozenType==='month'" label="背景漏失(m³/KM/月)" align="center"
                               prop="uarl" width="160"/>
              <el-table-column label="平均压力(MPa)" align="center" prop="avgPressure" width="140" sortable/>
              <!--      <el-table-column label="进水流量" align="center" prop="inletFlow"/>-->
              <!--      <el-table-column label="出水流量" align="center" prop="outletFlow"/>-->
              <!--      <el-table-column label="监控表用水量，总表关系中类型为“流量计”的表用水量之和" align="center" prop="jkFlow"/>-->
              <!--      <el-table-column label="户表用水量" align="center" prop="householdFlow"/>-->
              <!--      <el-table-column label="大用户用水量" align="center" prop="seniorFlow"/>-->
              <!--      <el-table-column label="消火栓用水量" align="center" prop="hydrantFlow"/>-->
              <!--      <el-table-column label="账期" align="center" prop="paydate"/>-->
            </el-table>

            <pagination
              :total="total"
              :page-sizes="[15, 30, 45, 60]"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
          </el-col>
        </el-row>
      </CustomBox>
    </ContentWrap>
  </div>
</template>

<script>
import {listByQueryModel} from "@/api/leakage/freezeDfq";
import RegionTree from "@/views/leakage/components/RegionTree.vue";

export default {
  name: "FreezeDfq",
  components: {RegionTree},
  data() {
    return {
      screenHeight: null,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 大分区冻结数据表格数据
      freezeDfqList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        areaId: null,
        frozenType: 'day',
        startDate: '',
        endDate: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      dateType: 'date',
      valueType: 'yyyy-MM-dd',
      editingZone: undefined
    };
  },
  created() {
    // this.getList();
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - 235
    },
  },
  methods: {
    /** 查询大分区冻结数据列表 */
    getList() {
      this.loading = true;
      if (this.editingZone) {
        this.queryParams.areaId = this.editingZone.areaId
      }
      listByQueryModel(this.queryParams).then(response => {
        this.freezeDfqList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('swla2/freezeDfq/export', {
        ...this.queryParams
      }, `大分区绩效报表.xlsx`)
    },
    handleFrozenTypeChange(val) {
      if (val === 'day') {
        this.dateType = 'date'
        this.valueType = 'yyyy-MM-dd'
      } else if (val === 'month') {
        this.dateType = 'month'
        this.valueType = 'yyyy-MM'
      } else {
        this.dateType = 'year'
        this.valueType = 'yyyy'
      }
    },
    handleDfqNodeClick(node) {
      const levelType = node.levelType
      if (levelType !== '1') {
        this.editingZone = undefined
        this.$message.error('请选择大分区节点')
      } else {
        this.editingZone = node
        this.handleQuery()
      }
    },
    getHbStyle(hb) {
      let color
      const fHb = parseFloat(hb)
      if (fHb > 0) {
        color = 'red'
      } else {
        color = 'green'
      }
      return {color: color}
    },
    getCxclStyle(cxcl) {
      let color
      const fCxcl = parseFloat(cxcl)
      if (fCxcl <= 0 || fCxcl > 30) {
        color = 'red'
      } else if (fCxcl > 0 && fCxcl < 10) {
        color = 'green'
      } else if (fCxcl > 10 && fCxcl < 20) {
        color = '#ffd400'
      } else if (fCxcl > 20 && fCxcl < 30) {
        color = '#ff9900'
      }

      return {color: color}
    }
  }
};
</script>
